package com.hema.auction.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.hema.auction.R;
import com.hema.auction.adapter.FragAdapter;
import com.hema.auction.base.BaseActivity;
import com.hema.auction.event.PayResultEvent;
import com.hema.auction.fragment.DifferenceOrderFragment;
import com.hema.auction.widget.view.TitleView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 差价购买
 * author by john
 */

public class DifferenceOrderActivity extends BaseActivity {

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private String[] title = new String[]{"全部", "待付款", "待发货", "待签收", "待晒单"};
    List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_difference_order);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        init();
    }

    private void init(){
        /**
         * 100:全部
         * 0:待付款
         * 1:待发货
         * 2:待签收
         * 3:待晒单
         */
        fragments.add(DifferenceOrderFragment.newInstance(100));
        fragments.add(DifferenceOrderFragment.newInstance(0));
        fragments.add(DifferenceOrderFragment.newInstance(1));
        fragments.add(DifferenceOrderFragment.newInstance(2));
        fragments.add(DifferenceOrderFragment.newInstance(3));
        FragAdapter fragAdapter = new FragAdapter(getSupportFragmentManager(), fragments, title);
        viewPager.setAdapter(fragAdapter);
        tablayout.setupWithViewPager(viewPager);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPayResultvent(PayResultEvent event) {
        if(event.getType() == 4) {
            viewPager.setCurrentItem(2);
            DifferenceOrderFragment auctionFragment = (DifferenceOrderFragment) fragments.get(2);
            auctionFragment.setRefresh();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
